﻿Imports System
Imports System.Security.Cryptography
Imports System.Text
Public Class CThanh_Utilities
    Public Shared Function getMd5Hash(ByVal input As String) As String
        ' Create a new instance of the MD5CryptoServiceProvider object. 
        Dim md5Hasher As New MD5CryptoServiceProvider()

        ' Convert the input string to a byte array and compute the hash. 
        Dim data As Byte() = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input))

        ' Create a new Stringbuilder to collect the bytes 
        ' and create a string. 
        Dim sBuilder As New StringBuilder()

        ' Loop through each byte of the hashed data  
        ' and format each one as a hexadecimal string. 
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i

        ' Return the hexadecimal string. 
        Return sBuilder.ToString()

    End Function
    ' Verify a hash against a string. 
    Public Shared Function verifyMd5Hash(ByVal input As String, ByVal hash As String) As Boolean
        ' Hash the input. 
        Dim hashOfInput As String = getMd5Hash(input)

        ' Create a StringComparer an compare the hashes. 
        Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase

        If 0 = comparer.Compare(hashOfInput, hash) Then
            Return True
        Else
            Return False
        End If
    End Function
    Public Shared Function ComputeSHA1(ByVal textToHash As String) As String
        Dim SHA1 As SHA1CryptoServiceProvider = New SHA1CryptoServiceProvider
        Dim byteV As Byte() = System.Text.Encoding.UTF8.GetBytes(textToHash)
        Dim byteH As Byte() = SHA1.ComputeHash(byteV)
        SHA1.Clear()
        Return Convert.ToBase64String(byteH)
    End Function
    Public Shared Function VerifySHA1Hash(ByVal input As String, ByVal hash As String) As Boolean
        ' Hash the input. 
        Dim hashOfInput As String = ComputeSHA1(input)

        ' Create a StringComparer an compare the hashes. 
        Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase

        If 0 = comparer.Compare(hashOfInput, hash) Then
            Return True
        Else
            Return False
        End If
    End Function
End Class
